//
//  BBuildingShopViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/13.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BBuildingShopViewController.h"
#import "WqGaoDeLocationViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "BaseNavViewController.h"
#import "TabBarViewController.h"

@interface BBuildingShopViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *logoBUt;
@property (weak, nonatomic) IBOutlet UITextField *shopNameTF;
@property (weak, nonatomic) IBOutlet UITextField *callPeraonNameTF;
@property (weak, nonatomic) IBOutlet UITextField *xiangxixinxiTF;
@property (weak, nonatomic) IBOutlet UITextField *phonetf;
@property (weak, nonatomic) IBOutlet UIButton *logoImageBut;
@property (weak, nonatomic) IBOutlet UIButton *addressBut;
@property (weak, nonatomic) IBOutlet UIImageView *myimage;
@property(nonatomic,strong)NSMutableDictionary *mydic;
@property(nonatomic,strong)UIImage *mylogoimage;
@property(nonatomic,assign)BOOL upimage;

@end

@implementation BBuildingShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"店铺信息";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.mydic = [NSMutableDictionary dictionaryWithCapacity:1];
    self.mylogoimage = [[UIImage alloc]init];
    
    self.upimage = YES;
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)uplogoAction:(UIButton *)sender {
    [self uplogo];
}

- (IBAction)uologoImageAction:(UIButton *)sender {
    [self uplogo];
}

- (IBAction)seleBUtAddress:(UIButton *)sender {
    [self seleaddress];
}

- (IBAction)seleImageAddres:(UIButton *)sender {
    [self seleaddress];
}

- (IBAction)finishAction:(UIButton *)sender {
    //完成
    if (self.upimage) {
        ShowToastWithText(@"请上传店铺图片");
        return;
    }
    KMyLog(@"sdfsdfsd%@",self.shopNameTF.text);
    if (strIsEmpty(self.shopNameTF.text)) {
        ShowToastWithText(@"请输入店铺名称");
        return;
    }
    
    if (strIsEmpty(self.callPeraonNameTF.text)) {
        ShowToastWithText(@"请输入联系人");
        return;
    }
    
    if (strIsEmpty(self.phonetf.text)) {
        ShowToastWithText(@"请输入联系电话");
        return;
    }
    
    if (![HFTools isMobileNumberOnly:_phonetf.text]) {
        ShowToastWithText(@"请输入正确的电话号码");
        return;
    }
    
    if ([_mydic allKeys].count <2) {
        ShowToastWithText(@"请选择地址");
        return;
    }
    
    if (strIsEmpty(self.xiangxixinxiTF.text)) {
        ShowToastWithText(@"请输入详细地址");
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    //param[@"type"] = @"logo";
    NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithCapacity:1];
    mudic[@"agent_id"]=  KUID;
    mudic[@"shop_name"]=  _shopNameTF.text;
    mudic[@"shop_address"]=  _xiangxixinxiTF.text;
    mudic[@"lon"]=  [_mydic objectForKey:@"longitude"];
    mudic[@"lat"]=   [_mydic objectForKey:@"latitude"];
    mudic[@"shop_phone"]=   _phonetf.text;
    mudic[@"shop_area"]=  [_mydic objectForKey:@"area"];;
    
    NSString *jsion = [HFTools toJSONString:mudic];
    
    param[@"data"] = jsion;
    
    [NetWorkTool UploadPicWithUrl:registerAgentShop param:param key:@"image" image:_mylogoimage withSuccess:^(id dic) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        ShowToastWithText(@"绑定成功");
        TabBarViewController *tabbar = [[TabBarViewController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(void)seleaddress{
    //选择地址
    if ([self isLocationServiceOpen]) {
        
        WqGaoDeLocationViewController *locaVC = [[WqGaoDeLocationViewController alloc]init];
        BaseNavViewController * navigation = [[BaseNavViewController alloc]initWithRootViewController:locaVC];
        //[self.logoBUt setTitle:[locaVC.locadic objectForKey:@"building_name"] forState:UIControlStateNormal];
        locaVC.locaBlock = ^(NSMutableDictionary *locadic) {
            [self.addressBut setTitle:[locadic objectForKey:@"building_name"] forState:UIControlStateNormal];
            [self.addressBut setTitleColor:K333333 forState:UIControlStateNormal];
            
            self.myimage.hidden = YES;
            //[self.locadic removeAllObjects];
            self.mydic = locadic;
            KMyLog(@"看看都有啥%@",locadic);
        };
        [self presentViewController:navigation animated:YES completion:^{
            //[self.locationButton setTitle:[locaVC.locadic objectForKey:@"building_name"] forState:UIControlStateNormal];
            //NSLog(@"确定kkkkkkkkkk00%@",locaVC.locadic);
            //self.locadic = locaVC.locadic;
        }];
    } else {
        //未开启定位权限
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"需要您开启定位权限,是否允许" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            KMyLog(@"取消");
        }]];
        [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            KMyLog(@"确定");
            if ([[[UIDevice currentDevice] systemVersion] integerValue] >9.9) {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            } else {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                //NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
                //[[UIApplication sharedApplication] openURL:url];
            }
        }]];
        [self presentViewController:alertCon animated:YES completion:nil];
    }
}

-(void)uplogo{
    //选择logo照片
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.allowsEditing = YES;
    pick.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:pick controller:self sourceView:nil];
}

- (void)upLoadHeadIcon:(UIImage *)image {
    self.logoBUt.hidden = YES;
    [self.logoImageBut setImage:image forState:(UIControlStateNormal)] ;
    self.mylogoimage = image;
    self.upimage = NO;
    //    [MBProgressHUD showHUDAddedTo:self.view msg:@"上传头像..." animated:YES];
    //    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //    //param[@"type"] = @"logo";
    //    [NetWorkTool UploadPicWithUrl:changeInfo param:param key:@"images" image:image withSuccess:^(id dic) {
    //        [MBProgressHUD hideHUDForView:self.view animated:YES];
    //        self.upimage = NO;
    //    } failure:^(NSError *error) {
    //        [MBProgressHUD hideHUDForView:self.view animated:YES];
    //    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    [self upLoadHeadIcon:image];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//是否开启定位
- (BOOL)isLocationServiceOpen {
    if ([ CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        return NO;
    } else
        return YES;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
